package com.mk.cloud.common.service.core;

import act.controller.Controller;
import com.alibaba.fastjson.JSON;
import com.mk.cloud.common.domain.CpConfig;
import com.mk.cloud.common.enums.ConfigCode;
import com.mk.cloud.common.enums.GlobalValues;
import com.mk.cloud.common.util.S2;
import org.apache.commons.lang3.StringUtils;
import org.osgl.$;
import org.osgl.Osgl;
import org.osgl.http.H;
import org.osgl.mvc.annotation.GetAction;
import org.osgl.mvc.result.Result;
import org.osgl.util.C;

import javax.inject.Inject;
import java.util.*;

@SuppressWarnings("unused")
public class ConfigService extends Controller.Base {
    @Inject
    private CpConfig.Mapper cpConfigMapper;


    private Osgl.T4<Map<String, LinkedHashMap<String, String>>, Map<String, List<List<String>>>, Map<String, Object>, Map<String, Object>> readConfigs() {
        //region 读取
        int i = 0, j = GlobalValues.values().length;
        Map<String, LinkedHashMap<String, String>> result = new HashMap<>();
        Map<String, List<List<String>>> result2 = new HashMap<>();
        while (i < j - 1) {
            i++;
            GlobalValues vv = GlobalValues.values()[i];
            String v_guess = S2.regexGet("_([^_]+)$", vv.name(), 1);
            if (StringUtils.equals(v_guess, vv.value())) {
                String v_name = vv.name().replaceAll("_" + v_guess, "");
                LinkedHashMap<String, String> v;
                List<List<String>> v2;
                if (!result.containsKey(v_name)) {
                    v = new LinkedHashMap<>();
                    v2 = new ArrayList<>();
                    result.put(v_name, v);
                    result2.put(v_name, v2);
                } else {
                    v = result.get(v_name);
                    v2 = result2.get(v_name);
                }
                v.put(vv.value(), vv.desc());
                List<String> vv2 = new ArrayList<>();
                vv2.add(vv.value());
                vv2.add(vv.desc());
                v2.add(vv2);
            }
        }

        Map<String, Object> sys_config = new HashMap<>();
        i = 0;
        j = ConfigCode.values().length;
        while (i < j) {
            ConfigCode vv = ConfigCode.values()[i];
            sys_config.put(vv.name(),vv.value(null));
            i++;
        }

        //其他配置,来自app.base
        Map<String, Object> configs = new HashMap<>();

        return new Osgl.T4<>(result, result2, configs, sys_config);
    }

    @GetAction("/GlobalValues.jsx")
    public String getConfigJs() {
        $.T4 t = readConfigs();
        String Glovalues = "var GlobalValues =" + JSON.toJSONString(t._1) + ";var GlobalConfigs = " + JSON.toJSONString(t._3) + ";" + "var GlobalValues2 = " + JSON.toJSONString(t._2) + ";" ;//+ "var SysConfig = " + JSON.toJSONString(t._4) + ";";
        context.resp().contentType(H.Format.JAVASCRIPT);
        return Glovalues;
    }

    @GetAction("/GlobalValues.json")
    public Result getConfigJson() {
        Osgl.T4 t = readConfigs();
        return renderJson(C.map("GlobalMap", t._1, "AppConfig", t._3, "GlobalArray", t._2, "CpConfig", t._4));
    }
}
